# -*-* encoding:UTF-8 -*-
# author            : mengy
# date              : 2018/12/13
# python-version    : Python 3.7.0
# description       : 多人聊天室-服务端

import asyncore
from com.python.demo.mutil_chatting_room.entities.ChatRoom import ChatRoom
from com.python.demo.mutil_chatting_room.entities.ChatSession import ChatSession


class ChatServer(asyncore.dispatcher):
    "聊天服务逻辑"

    def __init__(self, addr):
        asyncore.dispatcher.__init__(self)
        self.create_socket()
        self.set_reuse_addr()
        self.bind(addr)
        self.listen(5)  # 允许接入5个用户
        self.user = {}
        self.main_room = ChatRoom(self)

    def handle_accept(self):
        conn, addr = self.accept()
        ChatSession(self, conn)
